/**
 * Copyright 2015 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2015年9月21日 上午11:31:53
 */
package com.absir.appserv.system.asset;

import java.io.IOException;
import java.net.HttpURLConnection;

import com.absir.appserv.system.bean.JAgent;
import com.absir.appserv.system.helper.HelperClient;
import com.absir.appserv.system.helper.HelperString;
import com.absir.appserv.system.service.BeanService;
import com.absir.bean.basis.Base;
import com.absir.core.helper.HelperIO;
import com.absir.server.in.InMethod;
import com.absir.server.value.Body;
import com.absir.server.value.Server;
import com.absir.servlet.InputRequest;

/**
 * @author absir
 *
 */
@Base
@Server
public class Asset_agent extends AssetServer {

	/**
	 * @param channel
	 * @param input
	 * @throws IOException
	 */
	@Body
	public void channel(String channel, InputRequest inputRequest) throws IOException {
		JAgent agent = BeanService.ME.get(JAgent.class, channel);
		if (agent != null) {
			String url = inputRequest.getRequest().getQueryString();
			url = HelperString.isEmpty(url) ? agent.getUrl() : (agent.getUrl() + url);
			HttpURLConnection urlConnection = HelperClient.openConnection(url,
					inputRequest.getMethod() == InMethod.POST ? inputRequest.getInputStream() : null);
			HelperIO.copy(HelperClient.openConnection(urlConnection), inputRequest.getOutputStream());
		}
	}
}
